import { api } from '../api';
import type { HomeworkTable, PostLikeResponse, RequestTableParams } from './types/HomeworkDisplayTable';

// 获取作品集
export const postHomeworkTable = async (params: RequestTableParams): Promise<HomeworkTable> => {
  try {
    const response = await api.post<HomeworkTable>('/java/ground/culture/get_artworks', params);
    return response.data; // 返回后端响应的数据
  }
  catch (error) {
    console.error('Error fetching artworks:', error);
    throw error; // 抛出错误以便后续处理
  }
};

// 获取最常用的八个标签
export const getCommonHomeworkTag = async () => {
  try {
    const response = await api.get("/java/ground/culture/get_common_artworks_tags");
    return response.data;
  }
  catch (error) {
    throw new Error("用户信息请求失败");
  }
};

// 点赞
export const postLike = async (artworkId: string): Promise<boolean> => {
  try {
    const params = { artworkId };

    // 发送 POST 请求，并指定返回数据类型
    const response = await api.post<PostLikeResponse>('/java/ground/culture/add_artworks_like', params);

    if ((response as unknown as PostLikeResponse).code === 200)
      return true;
    return false;
  }
  catch (error) {
    console.error('Error fetching artworks:', error);
    throw error;
  }
};
